<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
        <!DOCTYPE html>
        <html lang="zh-CN">

        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>评价管理 - 商品评价系统</title>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
            <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
            <style>
                .sidebar {
                    min-height: 100vh;
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    color: white;
                }

                .sidebar .nav-link {
                    color: rgba(255, 255, 255, 0.8);
                    border-radius: 10px;
                    margin: 5px 0;
                    transition: all 0.3s;
                }

                .sidebar .nav-link:hover,
                .sidebar .nav-link.active {
                    color: white;
                    background: rgba(255, 255, 255, 0.2);
                }

                .main-content {
                    background: #f8f9fa;
                    min-height: 100vh;
                }

                .content-card {
                    background: white;
                    border-radius: 15px;
                    padding: 25px;
                    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
                    margin-bottom: 30px;
                }

                .user-avatar {
                    width: 40px;
                    height: 40px;
                    border-radius: 50%;
                    object-fit: cover;
                }

                .product-image {
                    width: 50px;
                    height: 50px;
                    border-radius: 8px;
                    object-fit: cover;
                }

                .status-badge {
                    font-size: 0.8rem;
                }

                .action-buttons .btn {
                    margin: 0 2px;
                    padding: 5px 10px;
                }

                .search-box {
                    background: white;
                    border-radius: 25px;
                    border: 1px solid #ddd;
                    padding: 10px 20px;
                }

                .filter-section {
                    background: white;
                    border-radius: 15px;
                    padding: 20px;
                    margin-bottom: 20px;
                }

                .table-responsive {
                    border-radius: 15px;
                    overflow: hidden;
                }

                .table th {
                    background: #f8f9fa;
                    border: none;
                    font-weight: 600;
                }

                .table td {
                    border: none;
                    vertical-align: middle;
                }

                .pagination {
                    justify-content: center;
                }

                .pagination .page-link {
                    border-radius: 50%;
                    margin: 0 2px;
                    border: none;
                    color: #667eea;
                }

                .pagination .page-item.active .page-link {
                    background: #667eea;
                    border-color: #667eea;
                }

                .rating-stars {
                    color: #ffc107;
                }

                .review-content {
                    max-width: 200px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }

                .review-detail {
                    max-height: 300px;
                    overflow-y: auto;
                }
            </style>
        </head>

        <body>
            <div class="container-fluid">
                <div class="row">
                    <!-- 侧边栏 -->
                    <nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
                        <div class="position-sticky pt-3">
                            <div class="text-center mb-4">
                                <h4><i class="fas fa-tachometer-alt me-2"></i>管理后台</h4>
                                <small class="text-muted">欢迎，<span id="adminName">管理员</span></small>
                            </div>

                            <ul class="nav flex-column">
<%--                                <li class="nav-item">--%>
<%--                                    <a class="nav-link" href="/product-review/page/admin/dashboard">--%>
<%--                                        <i class="fas fa-home me-2"></i>仪表板--%>
<%--                                    </a>--%>
<%--                                </li>--%>
                                <li class="nav-item">
                                    <a class="nav-link" href="/product-review/page/admin/users">
                                        <i class="fas fa-users me-2"></i>用户管理
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/product-review/page/admin/products">
                                        <i class="fas fa-box me-2"></i>商品管理
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link active" href="/product-review/page/admin/reviews">
                                        <i class="fas fa-comments me-2"></i>评价管理
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/product-review/page/index">
                                        <i class="fas fa-arrow-left me-2"></i>返回前台
                                    </a>
                                </li>
                                <li class="nav-item mt-3">
                                    <a class="nav-link" href="#" onclick="logout()">
                                        <i class="fas fa-sign-out-alt me-2"></i>退出登录
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </nav>

                    <!-- 主内容区 -->
                    <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
                        <!-- 顶部导航 -->
                        <div
                            class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                            <h1 class="h2">评价管理</h1>
                            <div class="btn-toolbar mb-2 mb-md-0">
                                <div class="btn-group me-2">
                                    <button type="button" class="btn btn-sm btn-outline-secondary"
                                        onclick="refreshReviews()">
                                        <i class="fas fa-sync-alt me-1"></i>刷新
                                    </button>
                                </div>
                                <div class="btn-group">
                                    <button type="button" class="btn btn-sm btn-warning"
                                        onclick="batchAuditReviews(1)">
                                        <i class="fas fa-check me-1"></i>批量通过
                                    </button>
                                    <button type="button" class="btn btn-sm btn-danger"
                                        onclick="batchAuditReviews(2)">
                                        <i class="fas fa-times me-1"></i>批量拒绝
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- 筛选和搜索 -->
                        <div class="filter-section">
                            <div class="row align-items-center">
                                <div class="col-md-3">
                                    <div class="input-group">
                                        <span class="input-group-text"><i class="fas fa-search"></i></span>
                                        <input type="text" class="form-control" id="searchInput" placeholder="搜索评价内容..."
                                            onkeyup="searchReviews()">
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <select class="form-select" id="statusFilter" onchange="filterReviews()">
                                        <option value="">全部状态</option>
                                        <option value="0">待审核</option>
                                        <option value="1">已通过</option>
                                        <option value="2">已拒绝</option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <select class="form-select" id="ratingFilter" onchange="filterReviews()">
                                        <option value="">全部评分</option>
                                        <option value="5">5星</option>
                                        <option value="4">4星</option>
                                        <option value="3">3星</option>
                                        <option value="2">2星</option>
                                        <option value="1">1星</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <div class="row">
                                        <div class="col-6">
                                            <input type="date" class="form-control" id="startDate"
                                                onchange="filterReviews()">
                                        </div>
                                        <div class="col-6">
                                            <input type="date" class="form-control" id="endDate"
                                                onchange="filterReviews()">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <button class="btn btn-outline-secondary w-100" onclick="resetFilters()">
                                        <i class="fas fa-undo me-1"></i>重置
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- 评价列表 -->
                        <div class="content-card">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="mb-0">评价列表</h5>
                                <div class="d-flex align-items-center">
                                    <span class="text-muted me-3">共 <span id="totalReviews">0</span> 条评价</span>
                                    <div class="btn-group btn-group-sm" role="group">
                                        <button type="button" class="btn btn-outline-primary"
                                            onclick="selectAllReviews()">
                                            全选
                                        </button>
                                        <button type="button" class="btn btn-outline-danger"
                                            onclick="batchDeleteReviews()">
                                            批量删除
                                        </button>
                                    </div>
                                </div>
                            </div>

                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th width="50">
                                                <input type="checkbox" id="selectAll" onchange="toggleSelectAll()">
                                            </th>
                                            <th width="80">用户</th>
                                            <th width="80">商品</th>
                                            <th>评价内容</th>
                                            <th width="100">评分</th>
                                            <th width="100">状态</th>
                                            <th width="120">创建时间</th>
                                            <th width="200">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="reviewsTableBody">
                                        <!-- 评价列表将通过JavaScript动态加载 -->
                                    </tbody>
                                </table>
                            </div>

                            <!-- 分页 -->
<%--                            <nav aria-label="评价分页">--%>
<%--                                <ul class="pagination" id="reviewsPagination">--%>
<%--                                    <!-- 分页将通过JavaScript动态生成 -->--%>
<%--                                </ul>--%>
<%--                            </nav>--%>
                        </div>
                    </main>
                </div>
            </div>

            <!-- 评价详情模态框 -->
            <div class="modal fade" id="reviewDetailModal" tabindex="-1">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">评价详情</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body" id="reviewDetailContent">
                            <!-- 评价详情内容将通过JavaScript动态加载 -->
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-success" id="approveBtn"
                                onclick="auditReview(1)">通过
                            </button>
                            <button type="button" class="btn btn-danger" id="rejectBtn"
                                onclick="auditReview(2)">拒绝
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 审核理由模态框 -->
            <div class="modal fade" id="auditReasonModal" tabindex="-1">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="auditReasonTitle">审核理由</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3">
                                <label for="auditReason" class="form-label">请输入审核理由：</label>
                                <textarea class="form-control" id="auditReason" rows="4"
                                    placeholder="请输入审核理由..."></textarea>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" onclick="confirmAudit()">确认</button>
                        </div>
                    </div>
                </div>
            </div>

            <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
            <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
            <script>
                // 全局变量
                let currentUser = null;
                let reviewDetailModal = null;
                let auditReasonModal = null;
                let allReviews = [];
                let filteredReviews = [];
                let currentPage = 1;
                let pageSize = 10;
                let currentReviewId = null;
                let currentAuditStatus = null;

                $(document).ready(function () {
                    // 初始化模态框
                    reviewDetailModal = new bootstrap.Modal(document.getElementById('reviewDetailModal'));
                    auditReasonModal = new bootstrap.Modal(document.getElementById('auditReasonModal'));

                    // 检查管理员权限
                    checkAdminPermission();

                    // 加载评价列表
                    loadReviews();
                });

                // 检查管理员权限
                function checkAdminPermission() {
                    $.ajax({
                        url: '/product-review/user/current',
                        type: 'GET',
                        success: function (result) {
                            if (result.code === 200 && result.data) {
                                currentUser = result.data;
                                if (result.data.role !== 'ADMIN') {
                                    alert('您没有管理员权限');
                                    window.location.href = '/product-review/page/index';
                                    return;
                                }
                                $('#adminName').text(result.data.username);
                            } else {
                                alert('请先登录');
                                window.location.href = '/product-review/page/login';
                            }
                        },
                        error: function () {
                            alert('请先登录');
                            window.location.href = '/product-review/page/login';
                        }
                    });
                }

                // 加载评价列表
                function loadReviews() {
                    $.ajax({
                        url: '/product-review/review/list',
                        type: 'GET',
                        success: function (result) {
                            if (result.code === 200) {
                                allReviews = result.data || [];
                                filteredReviews = [...allReviews];
                                updateReviewDisplay();
                            } else {
                                alert('加载评价列表失败：' + result.message);
                            }
                        },
                        error: function () {
                            alert('加载评价列表失败');
                        }
                    });
                }

                // 更新评价显示
                function updateReviewDisplay() {
                    $('#totalReviews').text(filteredReviews.length);

                    // 分页
                    const totalPages = Math.ceil(filteredReviews.length / pageSize);
                    const startIndex = (currentPage - 1) * pageSize;
                    const endIndex = startIndex + pageSize;
                    const pageReviews = filteredReviews.slice(startIndex, endIndex);

                    // 显示评价列表
                    displayReviews(pageReviews);

                    // 显示分页
                    displayPagination(totalPages);
                }

                // 显示评价列表
                function displayReviews(reviews) {
                    let html = '';

                    if (reviews.length === 0) {
                        html = '<tr><td colspan="8" class="text-center text-muted py-4">暂无评价数据</td></tr>';
                    } else {
                        reviews.forEach(function (review) {
                            const createTime = new Date(review.createTime).toLocaleDateString();
                            const statusBadge = getStatusBadge(review.status);
                            const ratingStars = generateStars(review.rating);
                            const content = review.content || '';
                            const shortContent = content.length > 30 ? content.substring(0, 30) + '...' : content;
                            let auditButtons = '';
                            if (review.status === 0) {
                                auditButtons = `                    <button class="btn btn-sm btn-outline-success" onclick="quickAudit(\${review.id}, 1)" title="通过">
                        <i class="fas fa-check"></i>
                    </button>
                    <button class="btn btn-sm btn-outline-warning" onclick="quickAudit(\${review.id}, 2)" title="拒绝">
                        <i class="fas fa-times"></i>
                    </button>`;
                            }

                            html += `
                        <tr>
                            <td>
                                <input type="checkbox" class="review-checkbox" value="\${review.id}">
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
<!--                                    <img src="/product-review/static/img/default-avatar.jpg" class="user-avatar me-2" alt="用户头像">-->
                                    <div>
                                        <div class="fw-bold">\${review.username}</div>
                                        <small class="text-muted">ID: \${review.userId}</small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <img src="\${review.productImageUrl}" class="product-image me-2" alt="商品图片">
                                    <div>
                                        <div class="fw-bold">\${review.productName}</div>
                                        <small class="text-muted">ID: \${review.productId}</small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="review-content" title="\${content}">\${shortContent}</div>
                            </td>
                            <td>
                                <div class="rating-stars">\${ratingStars}</div>
                                <small class="text-muted">\${review.rating}星</small>
                            </td>
                            <td>\${statusBadge}</td>
                            <td>\${createTime}</td>
                            <td>
                                <div class="action-buttons">
                                    <button class="btn btn-sm btn-outline-info" onclick="viewReviewDetail(\${review.id})" title="查看详情">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                \${auditButtons}

                                    <button class="btn btn-sm btn-outline-danger" onclick="deleteReview(\${review.id})" title="删除">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    `;
                        });
                    }

                    $('#reviewsTableBody').html(html);
                }

                // 获取状态徽章
                function getStatusBadge(status) {
                    const statusMap = {
                        0: { text: '待审核', className: 'bg-warning' },
                        1: { text: '已通过', className: 'bg-success' },
                        2: { text: '已拒绝', className: 'bg-danger' }
                    };

                    const statusInfo = statusMap[status] || { text: '未知', className: 'bg-secondary' };
                    return `<span class="badge \${statusInfo.className} status-badge">\${statusInfo.text}</span>`;
                }

                // 生成星级评分
                function generateStars(rating) {
                    let stars = '';
                    for (let i = 1; i <= 5; i++) {
                        if (i <= rating) {
                            stars += '<i class="fas fa-star"></i>';
                        } else {
                            stars += '<i class="far fa-star"></i>';
                        }
                    }
                    return stars;
                }

                // 显示分页
                function displayPagination(totalPages) {
                    let html = '';

                    if (totalPages <= 1) {
                        $('#reviewsPagination').html('');
                        return;
                    }

                    // 上一页
                    html += `
                <li class="page-item ${currentPage == 1 ? 'disabled' : ''}">
                    <a class="page-link" href="#" onclick="changePage(${currentPage - 1})">
                        <i class="fas fa-chevron-left"></i>
                    </a>
                </li>
            `;

                    // 页码
                    for (let i = 1; i <= totalPages; i++) {
                        if (i === currentPage || i === 1 || i === totalPages || (i >= currentPage - 1 && i <= currentPage + 1)) {
                            html += `
                        <li class="page-item ${i == currentPage ? 'active' : ''}">
                            <a class="page-link" href="#" onclick="changePage(${i})">${i}</a>
                        </li>
                    `;
                        } else if (i === currentPage - 2 || i === currentPage + 2) {
                            html += '<li class="page-item disabled"><span class="page-link">...</span></li>';
                        }
                    }

                    // 下一页
                    html += `
                <li class="page-item \${currentPage == totalPages ? 'disabled' : ''}">
                    <a class="page-link" href="#" onclick="changePage(\${currentPage + 1})">
                        <i class="fas fa-chevron-right"></i>
                    </a>
                </li>
            `;

                    $('#reviewsPagination').html(html);
                }

                // 切换页面
                function changePage(page) {
                    const totalPages = Math.ceil(filteredReviews.length / pageSize);
                    if (page >= 1 && page <= totalPages) {
                        currentPage = page;
                        updateReviewDisplay();
                    }
                }

                // 搜索评价
                function searchReviews() {
                    const keyword = $('#searchInput').val().toLowerCase();
                    const statusFilter = $('#statusFilter').val();
                    const ratingFilter = $('#ratingFilter').val();
                    const startDate = $('#startDate').val();
                    const endDate = $('#endDate').val();

                    console.log('筛选条件:', { keyword, statusFilter, ratingFilter, startDate, endDate });

                    filteredReviews = allReviews.filter(function (review) {
                        console.log('评价状态:', review.id, review.status, typeof review.status);
                        
                        const matchKeyword = !keyword ||
                            (review.content && review.content.toLowerCase().includes(keyword)) ||
                            (review.username && review.username.toLowerCase().includes(keyword)) ||
                            (review.productName && review.productName.toLowerCase().includes(keyword));

                        const matchStatus = !statusFilter || review.status === parseInt(statusFilter);
                        console.log('状态匹配结果:', review.id, matchStatus, review.status, parseInt(statusFilter));
                        
                        const matchRating = !ratingFilter || review.rating == ratingFilter;

                        let matchDate = true;
                        if (startDate || endDate) {
                            const reviewDate = new Date(review.createTime).toISOString().split('T')[0];
                            if (startDate && reviewDate < startDate) matchDate = false;
                            if (endDate && reviewDate > endDate) matchDate = false;
                        }

                        return matchKeyword && matchStatus && matchRating && matchDate;
                    });

                    console.log('筛选后评价数量:', filteredReviews.length);
                    currentPage = 1;
                    updateReviewDisplay();
                }

                // 筛选评价
                function filterReviews() {
                    searchReviews();
                }

                // 重置筛选
                function resetFilters() {
                    $('#searchInput').val('');
                    $('#statusFilter').val('');
                    $('#ratingFilter').val('');
                    $('#startDate').val('');
                    $('#endDate').val('');
                    filteredReviews = [...allReviews];
                    currentPage = 1;
                    updateReviewDisplay();
                }

                // 查看评价详情
                function viewReviewDetail(reviewId) {
                    const review = allReviews.find(r => r.id === reviewId);
                    if (!review) return;

                    currentReviewId = reviewId;

                    const createTime = new Date(review.createTime).toLocaleString();
                    const ratingStars = generateStars(review.rating);
                    const statusBadge = getStatusBadge(review.status);

                    const html = `
                <div class="review-detail">
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <h6>用户信息</h6>
                            <div class="d-flex align-items-center mb-2">
<!--                                <img src="/product-review/static/img/default-avatar.jpg" class="user-avatar me-3" alt="用户头像">-->
                                <div>
                                    <div class="fw-bold">\${review.username || '未知用户'}</div>
                                    <small class="text-muted">用户ID: \${review.userId}</small>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <h6>商品信息</h6>
                            <div class="d-flex align-items-center mb-2">
                                <img src="\${review.productImageUrl || 'https://iph.href.lu/200x200?fg=666666&bg=cccccc'}" class="product-image me-3" alt="商品图片">
                                <div>
                                    <div class="fw-bold">\${review.productName || '未知商品'}</div>
                                    <small class="text-muted">商品ID: \${review.productId}</small>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row mb-3">
                        <div class="col-md-4">
                            <h6>评分</h6>
                            <div class="rating-stars">\${ratingStars}</div>
                            <small class="text-muted">\${review.rating} 星</small>
                        </div>
                        <div class="col-md-4">
                            <h6>状态</h6>
                            \${statusBadge}
                        </div>
                        <div class="col-md-4">
                            <h6>创建时间</h6>
                            <div>\${createTime}</div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <h6>评价内容</h6>
                        <div class="border rounded p-3 bg-light">
                            \${review.content || '无评价内容'}
                        </div>
                    </div>
                    
                    ` + (review.auditReason ? `
                        <div class="mb-3">
                            <h6>审核理由</h6>
                            <div class="border rounded p-3 bg-light">
                                ` + review.auditReason + `
                            </div>
                        </div>
                    ` : '') + `
                </div>
            `;

                    $('#reviewDetailContent').html(html);

                    // 根据状态显示/隐藏审核按钮
                    if (review.status === 0) {
                        $('#approveBtn, #rejectBtn').show();
                    } else {
                        $('#approveBtn, #rejectBtn').hide();
                    }

                    reviewDetailModal.show();
                }

                // 快速审核
                function quickAudit(reviewId, status) {
                    currentReviewId = reviewId;
                    currentAuditStatus = status;

                    $('#auditReasonTitle').text(status === 1 ? '通过审核' : '拒绝审核');
                    $('#auditReason').val('');
                    auditReasonModal.show();
                }

                // 从详情页面审核
                function auditReview(status) {
                    currentAuditStatus = status;
                    reviewDetailModal.hide();

                    $('#auditReasonTitle').text(status === 1 ? '通过审核' : '拒绝审核');
                    $('#auditReason').val('');
                    auditReasonModal.show();
                }

                // 确认审核
                function confirmAudit() {
                    const reason = $('#auditReason').val().trim();

                    if (!reason) {
                        alert('请输入审核理由');
                        return;
                    }

                    $.ajax({
                        url: `/product-review/review/audit/${currentReviewId}`,
                        type: 'PUT',
                        contentType: 'application/json',
                        data: JSON.stringify({
                            status: currentAuditStatus,
                            auditReason: reason
                        }),
                        success: function (result) {
                            if (result.code === 200) {
                                alert('审核成功');
                                auditReasonModal.hide();
                                loadReviews();
                            } else {
                                alert('审核失败：' + result.message);
                            }
                        },
                        error: function () {
                            alert('审核失败');
                        }
                    });
                }

                // 批量审核
                function batchAuditReviews(status) {
                    const selectedIds = [];
                    $('.review-checkbox:checked').each(function () {
                        const reviewId = parseInt($(this).val());
                        const review = allReviews.find(r => r.id === reviewId);
                        if (review && review.status === 0) {
                            selectedIds.push(reviewId);
                        }
                    });

                    if (selectedIds.length === 0) {
                        alert('请选择待审核的评价');
                        return;
                    }

                    const reason = prompt(`请输入${status == 1 ? '通过' : '拒绝'}理由：`);
                    if (!reason) return;

                    $.ajax({
                        url: '/product-review/review/batch-audit',
                        type: 'PUT',
                        contentType: 'application/json',
                        data: JSON.stringify({
                            reviewIds: selectedIds,
                            status: status,
                            auditReason: reason
                        }),
                        success: function (result) {
                            if (result.code === 200) {
                                alert('批量审核成功');
                                loadReviews();
                                $('#selectAll').prop('checked', false);
                            } else {
                                alert('批量审核失败：' + result.message);
                            }
                        },
                        error: function () {
                            alert('批量审核失败');
                        }
                    });
                }

                // 删除评价
                function deleteReview(reviewId) {
                    if (confirm('确定要删除该评价吗？此操作不可恢复！')) {
                        $.ajax({
                            url: `/product-review/review/delete/${reviewId}`,
                            type: 'DELETE',
                            success: function (result) {
                                if (result.code === 200) {
                                    alert('评价删除成功');
                                    loadReviews();
                                } else {
                                    alert('删除失败：' + result.message);
                                }
                            },
                            error: function () {
                                alert('删除失败');
                            }
                        });
                    }
                }

                // 全选/取消全选
                function toggleSelectAll() {
                    const selectAll = $('#selectAll').prop('checked');
                    $('.review-checkbox').prop('checked', selectAll);
                }

                // 选择所有评价
                function selectAllReviews() {
                    $('.review-checkbox').prop('checked', true);
                    $('#selectAll').prop('checked', true);
                }

                // 批量删除评价
                function batchDeleteReviews() {
                    const selectedIds = [];
                    $('.review-checkbox:checked').each(function () {
                        selectedIds.push(parseInt($(this).val()));
                    });

                    if (selectedIds.length === 0) {
                        alert('请选择要删除的评价');
                        return;
                    }

                    if (confirm(`确定要删除选中的 ${selectedIds.length} 条评价吗？此操作不可恢复！`)) {
                        $.ajax({
                            url: '/product-review/review/batch-delete',
                            type: 'DELETE',
                            contentType: 'application/json',
                            data: JSON.stringify(selectedIds),
                            success: function (result) {
                                if (result.code === 200) {
                                    alert('批量删除成功');
                                    loadReviews();
                                    $('#selectAll').prop('checked', false);
                                } else {
                                    alert('批量删除失败：' + result.message);
                                }
                            },
                            error: function () {
                                alert('批量删除失败');
                            }
                        });
                    }
                }

                // 刷新评价列表
                function refreshReviews() {
                    loadReviews();
                }

                // 退出登录
                function logout() {
                    $.ajax({
                        url: '/product-review/user/logout',
                        type: 'POST',
                        success: function (result) {
                            if (result.code === 200) {
                                window.location.href = '/product-review/page/index';
                            } else {
                                alert('退出登录失败：' + result.message);
                            }
                        },
                        error: function () {
                            alert('退出登录失败');
                        }
                    });
                }
            </script>
        </body>

        </html>